package com.atguigu.eduService.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduService.client.VodClient;
import com.atguigu.eduService.entity.EduChapter;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.entity.vo.ChapterVo;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.eduService.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-15
 */
@Api(tags = "小节管理")
@RestController
@RequestMapping("/eduService/eduVideo")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;


    @ApiOperation(value = "添加小节信息")
    @PostMapping("addVideo")
    public R addChapter(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    @ApiOperation(value = "根据id删除小节")
    @DeleteMapping("delVideo/{id}")
    public R delChapter(@PathVariable String id) {
        EduVideo video = eduVideoService.getById(id);
        String videoId = video.getVideoSourceId();
        if (videoId != null) {
            vodClient.delVideo(id);
        }
        eduVideoService.removeById(id);
        return R.ok();

    }

    @ApiOperation(value = "根据id查询小节")
    @GetMapping("getVideoById/{id}")
    public R getVideoById(@PathVariable String id) {
        EduVideo videoById = eduVideoService.getById(id);
        return R.ok().data("chapterById", videoById);
    }

    @ApiOperation(value = "更新章节信息")
    @PostMapping("updateVideo")
    public R updateChapter(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }


}
